﻿using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;

using RichServerInfo.DeviceInfo;
using RichWebServiceDll;
using Common;
using EncryptUtily;
namespace RichECMS
{
    public partial class Regist : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var objSysinfo = new SystemInfo();
                txtHarDisk.Text = EncryptUtily.EncryptUtily.LoginEncrypt(objSysinfo.GetComputerBoard());
                txtDataTime.Text = DateTime.Now.ToString(CultureInfo.InvariantCulture);
                txtWebPath.Text = HttpContext.Current.Server.MapPath("~/");

                txtHarDisk.Attributes["readonly"] = "readonly";
                txtDataTime.Attributes["readonly"] = "readonly";
                txtWebPath.Attributes["readonly"] = "readonly";
            }
            else
            {
                //ibtnSave.Enabled = false;
            }
        }
        
        protected void ibtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtWebName.Text.Trim() == "" || txtWebUrl.Text.Trim() == "")
            {
                Alert("网站名称和网站域名不能为空");
                return;
            }
            using (ServerVariableFactory objServerVariableFactory = new ServerVariableFactory())
            {
                try
                {
                    ServerVariable objServerVariable = objServerVariableFactory.NewInstance();

                    objServerVariable.DataItem.chvServerIp = txtServerIp.Text;
                    objServerVariable.DataItem.intIsDrop = (int)Utility.IsDrop.No;
                    objServerVariable.DataItem.intLsTryUse = (int)Utility.IsTryUse.Yes;
                    objServerVariable.DataItem.intIsAllMoney = (int)Utility.IsAllMoney.Yes;
                    //通过本页面注册的CMS都为使用版,周期为7天，网站到期日是但前注册日期后推7天
                    objServerVariable.DataItem.dtmWebEnd = DateTime.Now.AddDays(+7);

                    

                    //向服务器发送注册信息
                    RichWSRegist objRichWSRegist = new RichWSRegist(txtServerIp.Text, txtWebName.Text, txtWebUrl.Text, txtHarDisk.Text);
                    if (int.Parse(objRichWSRegist.RequestResultState) == 1)
                    {
                        objServerVariable.DataItem.chvImpowerNo = objRichWSRegist.RequestImpowerNo;

                        objServerVariable.Insert();

                        AlertAndRedirect("恭喜您，用户注册成功！欢迎使用锐旗CMS网站管理系统！","Login.aspx",false);

                    }
                    else
                    {
                        Alert("服务器错误："+objRichWSRegist.RequestResultMessage);
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.Message.ToString());
                }
            }
        }

        protected void lbtnGetSIp_Click(object sender, EventArgs e)
        {
            //通过WebServices过去服务器Ip地址
            RichWSUrl objUrl = new RichWSUrl(AppConfig.GetServerUrl());
            txtServerIp.Text = objUrl.RequestResultMessage;
        }
    }
}